<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-button type="primary" @click="addCate">添加商品</el-button>
      <!-- 网格表格 -->
      <tree-gird index-text="#" border show-index :selection-type="false"  :expand-type="false" :data="cates" :columns="column">
        <template slot-scope="scope" slot="isOk">
          <i v-if="scope.row.cat_deleted" class="el-icon-error" style="color: red"></i>
          <i v-else  class="el-icon-success" style="color: green"></i>
        </template>
         <template slot-scope="scope" slot="order">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level === 1" type="success">二级</el-tag>
          <el-tag v-else type="warning">三级</el-tag>
        </template>
        <template slot="set">
          <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
      </tree-gird>
      <!-- 分页 -->
       <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryData.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!-- 点击添加分类的对话框 -->
    <el-dialog ref="addCateRef" @close="closeAddCate" title="提示" :visible.sync="addCateDialogVisible" width="50%">
      <el-form ref="addCateFormRef" :model="addCateForm" label-width="80px" :rules="addCateRules">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            v-model="cateValue"
            :options="parentCate"
            :props="cascaderProp"
            @change="cascaderHandleChange">
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCateSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 添加分类表单数据
      addCateForm: {
        cat_name: '',
        // 父级分类的Id
        cat_pid: 0,
        // 分类的等级，默认要添加的是1级分类
        cat_level: 0
      },
      // 添加分类对话框的显示与隐藏
      addCateDialogVisible: false,
      // 查询的数据
      queryData: {
        pagenum: 1,
        pagesize: 10
      },
      cates: [],
      total: 0,
      // 配置网格的数据
      column: [
        { label: '分类名称', prop: 'cat_name' },
        { label: '是否有效', type: 'template', template: 'isOk' },
        { label: '排序', type: 'template', template: 'order' },
        { label: '操作', type: 'template', template: 'set', width: '200' }
      ],
      // 验证规则
      addCateRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ]
      },
      // 两层分类数据
      parentCate: [],
      cascaderProp: {
        expandTrigger: 'hover',
        label: 'cat_name',
        value: 'cat_id',
        checkStrictly: true
      },
      cateValue: []
    }
  },
  methods: {
    // 获取商品分类数据
    async getGoodsCate () {
      const { data: res } = await this.$axios.get('categories', { params: this.queryData })
      if (res.meta.status !== 200) return this.$message.error('获取商品列表失败！')
      this.cates = res.data.result
      this.total = res.data.total
      console.log()
    },
    // 点击改变显示多少条
    handleSizeChange (newSize) {
      this.queryData.pagesize = newSize
      this.getGoodsCate()
    },
    // 点击分页显示第几页数据
    handleCurrentChange (newPage) {
      this.queryData.pagenum = newPage
      this.getGoodsCate()
    },
    // 添加分类
    async addCate () {
      this.addCateDialogVisible = true
      const { data: res } = await this.$axios.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) return this.$message.error('获取分类失败！')
      this.parentCate = res.data
      // console.log(res)
    },
    // 级联选择器
    cascaderHandleChange () {
      if (this.cateValue.length > 0) {
        this.addCateForm.cat_leval = this.cateValue[this.cateValue.length - 1]
        this.addCateForm.cat_pid = this.cateValue.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_leval = 0
      }
    },
    // 添加分类对话框关闭事件
    closeAddCate () {
      this.$refs.addCateFormRef.resetFields()
      this.cateValue = []
      this.addCateForm.cat_leval = 0
      this.addCateForm.cat_pid = 0
    },
    // 提交添加分类的信息
    addCateSubmit () {
      this.$refs.addCateFormRef.validate(async valid => {
        if (!valid) return this.$message.error('请先填写必填信息！')
        const { data: res } = await this.$axios.post('categories', this.addCateForm)
        if (res.meta.status !== 201) return this.$message.error('添加分类信息失败！')
        this.addCateDialogVisible = false
        this.$message.success('添加分类成功！')
        this.getGoodsCate()
      })
    }
  },
  created () {
    this.getGoodsCate()
  }
}
</script>

<style lang='less' scoped>
.el-cascader{
  width: 100%;
}
</style>
